using System;
using System.Collections.Generic;

namespace Rocuna.HID.Core
{
    /// <summary>
    /// Mapping between objects as values and actions to do after them.
    /// </summary>
    public class ActionsDictionary
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionsDictionary"/> class.
        /// </summary>
        public ActionsDictionary()
        {
            ActionsMapping = new Dictionary<object, List<Action<object>>>();
        }

        #endregion

        #region Properties

        private Dictionary<object, List<Action<object>>> ActionsMapping { get; set; }

        #endregion

        #region


        /// <summary>
        /// Gets or sets the Action Mapping dictionary with the specified indexer.
        /// </summary>
        public List<Action<object>> this[object indexer]
        {
            get
            {
                List<Action<object>> actions;
                ActionsMapping.TryGetValue(indexer, out actions);
                return actions;
            }
            set { ActionsMapping[indexer] = value; }
        }


        /// <summary>
        /// Adds the specified event type to action mapping.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="action">The action.</param>
        public void Add(object eventType, Action<object> action)
        {
            var actions = GetEntry(eventType);
            actions.Add(action);
        }

        private List<Action<object>> GetEntry(object eventType)
        {
            List<Action<object>> actions = new List<Action<object>>();
            if (this.ActionsMapping.TryGetValue(eventType, out actions))
                return actions;
            else
                ActionsMapping.Add(eventType, actions);
            return actions;
        }

        /// <summary>
        /// Removes the specified event type.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        public void Remove(object eventType)
        {
            ActionsMapping.Remove(eventType);
        }

        /// <summary>
        /// Removes the specified action from event type.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="action">The action.</param>
        public void Remove(object eventType, Action<object> action)
        {
            ActionsMapping[eventType].Remove(action);
        }

        #endregion
    }
}